package spring.boot.admin.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录失败处理器 handler
 */
@Slf4j
public class LoginFailHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
                                        AuthenticationException e) throws IOException, ServletException {
        log.error("登录失败！ 用户名或者密码有误！ip=[{}]  username=[{}] exception=[{}]",
                request.getRemoteAddr(), request.getParameter("username"), e.getMessage());
        response.sendRedirect("/login?error");
    }
}
